<?php
/**
 +------------------------------------------------------------------------------
 * Kit套件操作
 +------------------------------------------------------------------------------
 * @package  app
 * @author   alpha <QQ:411083658>
 * @date 2013-9-12
 * @time  下午13:38:07
 +------------------------------------------------------------------------------
 */
class Kit {
	protected $dao,$daovesting,$daolog,$post;
	protected $infoid=0;
	protected $debug=array();
	protected $isdebug = 0,$isecho = 0;
	/**
	 * 实例化分销商用户信息类函数
	 * */
	function __construct(){
		$this->dao = M('KitProduct');
		$this->daovesting = M('KitVesting');
		$this->daolog = M('KitLog');
	}
	/**
	 * 套件列表
	 * @param array     $_arr=array(
	 * 					'userid'=>套件归属用户Id,
	 * 					'cloudsn'=>套件编码,
	 * 					'type'=>套件类型,
	 * 					'province' => 省份ID，
	 * 					'city' => 城市ID，
	 * 					'area' => 区域ID
	 * 					'isclose'=>套件状态
	 * 					)
	 * @return array
	 * */
	 function kit_list($_arr=array(),$limit = '0,20',$order = 'id DESC'){
		$model = D('Kit');
		$where = array();
		if($_arr != 'all'){
			foreach ($_arr as $key => $value) {
				if($key == 'type'){
					$key = 'kp.type';
				}
				$where[$key] = $value;
			}
		}
		if (strtolower ( $limit ) == 'all') {
			$rel = $model->where($where)->count ();
			//dump($model->getLastSql());
			return $rel;
		}
		$rel = $model->where($where)->order ( $order )->limit ( $limit )->select();
		//dump($model->getLastSql());
		return $rel;
	 }
	 /**
	 * 我开通的套件列表
	 * @param array     $_arr=array(
	 * 					'userid'=>套件归属用户Id,
	 * 					'cloudsn'=>套件编码,
	 * 					'type'=>套件类型,
	 * 					'province' => 省份ID，
	 * 					'city' => 城市ID，
	 * 					'area' => 区域ID
	 * 					'isclose'=>套件状态
	 * 					)
	 * @return array
	 * */
	 function kit_open_list($_arr=array(),$limit = '0,20',$order = 'id DESC'){
		$model = D('Kitopen');
		$where = array();
		if($_arr != 'all'){
			foreach ($_arr as $key => $value) {
				if($key == 'type'){
					$key = 'kp.type';
				}
				$where[$key] = $value;
			}
		}
		if (strtolower ( $limit ) == 'all') {
			$rel = $model->where($where)->count ();
			return $rel;
		}
		$rel = $model->where($where)->order ( $order )->limit ( $limit )->select();
		//dump($model->getLastSql());
		return $rel;
	 }
	/**
	 * 获取套件信息
	 * @param int     $id 套件ID
	 * @return array
	 * */
	 function get_kit_detail($id){
		$this->dao = M('KitProduct');
		return $this->select($id);
	 }
	/**
	 * 套件归属列表
	 * @param array     $_arr=array(
	 * 					'kitid'=>套件Id
	 * 					)
	 * @return array
	 * */
	 function kit_vesting_list($_arr=array(),$limit = '0,20',$order = 'id DESC',$isall = true){
		$model = M('KitVesting');
		$where = '';
		if($_arr != 'all'){
			foreach ($_arr as $key => $value) {
				if(empty($where)){
					$where = $key."='".$value."'";
				}else{
					$where .= " AND ".$key."='".$value."'";
				}
			}
		}
		if($isall !== false){
			if (strtolower ( $limit ) == 'all') {
				return $model->where($where)->count ();
			}
			$rel = $model->where($where)->order ( $order )->limit ( $limit )->select();
			return $rel;
		}else{
			$rel = $model->where($where)->order ( $order )->select();
			return $rel;
		}
	 }
	/**
	 * 套件日志列表
	 * @param array     $_arr=array(
	 * 					'kitid'=>套件Id
	 * 					)
	 * @return array
	 * */
	 function kit_log_list($_arr=array(),$limit = '0,20',$order = 'id DESC',$isall = true){
		$model = M('KitLog');
		$where = '';
		if($_arr != 'all'){
			foreach ($_arr as $key => $value) {
				if(empty($where)){
					$where = $key."='".$value."'";
				}else{
					$where .= " AND ".$key."='".$value."'";
				}
			}
		}
		if($isall !== false){
			if (strtolower ( $limit ) == 'all') {
				return $model->where($where)->count ();
			}
			$rel = $model->where($where)->order ( $order )->limit ( $limit )->select();
			return $rel;
		}else{
			$rel = $model->where($where)->order ( $order )->select();
			return $rel;
		}
	 }
	 /**
	  * 添加套件
	  * @param array     $info=array(
	  * 					'cloudsn' => 套件编码,
	  * 					'type' => 套件类型
	  * 					)
	  * @param array 	$field_exist	设置不可重复的字段
	  * @param array 	$field_null		设置不可为空的字段
	  * @param int	 	$userid			归属用户编号
	  * @param int	 	$roleid			归属用户组编号
	  * @param int	 	$action_userid	操作人编号
	  * @param varchar 	$remark			操作特殊备注
	  * @return array 	成功返回 {"status":"1","msg":"信息","data":data,"id":序列号} 失败返回 {"status":"0","msg":"信息","data":data}
	  * */
	function kit_add($info=array(),$field_exist=array(),$field_null=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null){
		$this->dao = M('KitProduct');
		if(intval($userid)<=0||intval($action_userid)<=0||intval($roleid)<=0){
			return $this->_err("用户和用户组有误!");
		}	
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		foreach ($field_exist as $key => $value) {
			if(false == $this->field_exist($key, $info[$key])){
				return $this->_err($value."已存在");
			}
		}
		$this->dao = M('KitProduct');
		unset($this->post);
		$this->post = $info;
		$resid = $this->insert();
		if(false !== $resid){
			$vesting['kitid'] = $resid;
			$vesting['type'] = $roleid;
			$vesting['userid'] = $userid;
			$vesting['ctime'] = time();
			$this->insert_ext($this->daovesting,$vesting);
			$log['kitid'] = $resid;
			$log['type'] = 1;
			$log['userid'] = $action_userid;
			$log['ctime'] = time();
			if($remark != null){
				$log['remark'] = $remark;
			}
			$this->insert_ext($this->daolog,$log);
			$res=array('id'=>$resid);
			return $this->_suc($res,'创建成功');
		}else{
			return $this->_err("创建失败请重试");
		}
	}
	/**
	 * 修改套件
	 * @param array    	$info			要更新的数据
	 * @param array 	$field_exist	设置不可重复的字段
	 * @param array 	$field_null		设置不可为空的字段
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @return array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function kit_edit($info=array(),$field_exist=array(),$field_null=array(),$action_userid=0,$remark=null){
		$this->dao = M('KitProduct');
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		foreach ($field_exist as $key => $value) {
			if(false == $this->field_exist($key, $info[$key])){
				return $this->_err($value."已存在");
			}
		}
		unset($this->post);
		$this->post['id'] = $this->infoid;
		$this->post = $info;
		$status = $this->update();
		if(false !== $status){
			$log['kitid'] = $this->infoid;
			$log['type'] = 3;
			$log['userid'] = $action_userid;
			$log['ctime'] = time();
			if($remark != null){
				$log['remark'] = $remark;
			}
			$this->insert_ext($this->daolog,$log);
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'修改成功');
		}else{
			return $this->_err("修改失败请重试");
		}
	}
	function Kit_edit_cloudsn($cloudsn,$id){
		return $this->update_ext(M('KitProduct'),array('cloudsn'=>$cloudsn),$id);
	}
	/**
	 * 设置套件归属
	 * @param array     $info=array(
	 * 					'id' => 套件ID,
	 * 					'cloudsn' => 套件编码,
	 * 					'type' => 套件类型,
	 * 					'province_cn' => 中文,
	 * 					'province' => 省份ID,
	 * 					'city_cn' => 中文,
	 * 					'city' => 城市ID,
	 * 					'area_cn' => 中文,
	 * 					'area' => 区域ID
	 * 					)
	 * @param array 	$field_exist	设置不可重复的字段
	 * @param array 	$field_null		设置不可为空的字段
	 * @param int	 	$userid			归属用户编号
	 * @param int	 	$roleid			归属用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @return array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function kit_vesting($info=array(),$field_exist=array(),$field_null=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null){
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		foreach ($field_exist as $key => $value) {
			if(false == $this->field_exist($key, $info[$key])){
				return $this->_err($value."已存在");
			}
		}
		$this->dao = M('KitProduct');
		unset($this->post);
		$this->post = $info;
		$this->post['id'] = $this->infoid;
		$cloudsn = $this->build_cloudsn(array(
			  					'id' => $info['id'],
			  					'cloudsn' => $info['cloudsn'],
			  					'roleid' => $roleid,
			  					'type' => $info['type'],
			  					'province' => $info['province_cn'],
			  					'city' => $info['city_cn'],
			  					'area' => $info['area_cn'],
			  					'userid' => $userid
			  					),array());
		$this->post['cloudsn'] = $cloudsn;
		$status = $this->update();
		if(false !== $status){
			$vesting['kitid'] = $this->infoid;
			$vesting['type'] = $roleid;
			$vesting['userid'] = $userid;
			$vesting['ctime'] = time();
			$this->insert_ext($this->daovesting,$vesting);
			$log['kitid'] = $this->infoid;
			$log['type'] = 2;
			$log['userid'] = $action_userid;
			$log['ctime'] = time();
			if($remark != null){
				$log['remark'] = $remark;
			}
			$this->insert_ext($this->daolog,$log);
			//更新相关套件归属设置
			$res=array('mStatus'=>$status,'cloudsn'=>$cloudsn);
			return $this->_suc($res,'归属成功');
		}else{
			return $this->_err("归属失败请重试");
		}
	}
	/**
	 * 设置套件包归属
	 * @param array     $info=array(
	 * 					'id' => 套件包ID,
	 * 					'cloudsn' => 套件编码,
	 * 					'arr_id' => 套件ID数组,
	 * 					'type' => 套件类型,
	 * 					'province' => 省份ID,
	 * 					'city' => 城市ID,
	 * 					'area' => 区域ID
	 * 					)
	 * @param array 	$field_exist	设置不可重复的字段
	 * @param array 	$field_null		设置不可为空的字段
	 * @param int	 	$userid			归属用户编号
	 * @param int	 	$roleid			归属用户组编号
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @return array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function kits_vesting($info=array(),$field_exist=array(),$field_null=array(),$userid=0,$roleid=0,$action_userid=0,$remark=null){
		$this->dao = M('KitProduct');
		unset($this->post);
		$this->infoid = $info['id'];
		$arr_id = explode('|', $info['arr_id']);
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		foreach ($field_exist as $key => $value) {
			if(false == $this->field_exist($key, $info[$key])){
				return $this->_err($value."已存在");
			}
		}
		$this->post['id'] = $this->infoid;
		$this->post['cloudsn'] = $info['cloudsn'];
		$this->post['userid'] = $userid;
		$this->post['province'] = $info['province'];
		$this->post['city']	= $info['city'];
		$this->post['area'] = $info['area'];
		$this->post = $info;
		$status = $this->update();
		if(false !== $status){
			$vesting['kitid'] = $this->infoid;
			$vesting['type'] = $roleid;
			$vesting['userid'] = $userid;
			$vesting['ctime'] = time();
			$this->insert_ext($this->daovesting,$vesting);
			$log['kitid'] = $this->infoid;
			$log['type'] = 2;
			$log['userid'] = $action_userid;
			$log['ctime'] = time();
			if($remark != null){
				$log['remark'] = $remark;
			}
			$this->insert_ext($this->daolog,$log);
			//更新相关套件归属设置
			foreach ($arr_id as $key => $value) {
				$csn=$this->select_ext('',array('id' => $value),'cloudsn',false);
				$this->kit_vesting(array(
	  					'id' => $value,
	  					'cloudsn' => $this->build_cloudsn(array(
				  					'id' => $value,
				  					'cloudsn' => $csn,
				  					'roleid' => $roleid,
				  					'type' => 1,
				  					'province' => $info['province'],
				  					'city' => $info['city'],
				  					'area' => $info['area'],
				  					'userid' => $userid
				  					),array()),
	  					'province' => $info['province'],
	  					'city' => $info['city'],
	  					'area' => $info['area']
	  					),array(),array(),$userid,$roleid,$action_userid,$remark);
			}
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'归属成功');
		}else{
			return $this->_err("归属失败请重试");
		}
	}
	/**
	 * 更新状态
	 * @param array     $info=array(
	 * 					'id' => 套件ID,
	 * 					'isclose' => 开通状态
	 * 					)
	 * @param array 	$field_exist	设置不可重复的字段
	 * @param array 	$field_null		设置不可为空的字段
	 * @param int	 	$action_userid	操作人编号
	 * @param varchar 	$remark			操作特殊备注
	 * @return array 	成功返回 {"status":"1","msg":"信息","data":data,"mStatus":是否执行修改} 失败返回 {"status":"0","msg":"信息","data":data}
	 * */
	function kit_isclose($info=array(),$field_exist=array(),$field_null=array(),$action_userid=0,$remark=null){
		$this->dao = M('KitProduct');
		unset($this->post);
		$this->infoid = $info['id'];
		if(empty($this->infoid)){
			return $this->_err("没有要修改的数据或数据ID不可为空");
		}
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		foreach ($field_exist as $key => $value) {
			if(false == $this->field_exist($key, $info[$key])){
				return $this->_err($value."已存在");
			}
		}
		$this->post['id'] = $this->infoid;
		$this->post = $info;
		$status = $this->update();
		if(false !== $status){
			$log['kitid'] = $this->infoid;
			$log['type'] = 4;
			$log['userid'] = $action_userid;
			$log['ctime'] = time();
			if($remark != null){
				$log['remark'] = $remark;
			}
			$this->insert_ext($this->daolog,$log);
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'开通成功');
		}else{
			return $this->_err("开通失败请重试");
		}
	}
	/**
	 * 生成套件编码
	 * @param array     $info=array(
	 * 					'id' => 套件ID,
	 * 					'cloudsn' => 套件编码,
	 * 					'roleid' => 归属用户组ID,
	 * 					'type' => 套件类型,
	 * 					'province' => 省份名称,
	 * 					'city' => 城市名称,
	 * 					'area' => 区域名称,
	 * 					'userid' => 归属用户
	 * 					)
	 * @param array 	$field_null		设置不可为空的字段
	 * @return string	生成后的套件编码
	 * */
	function build_cloudsn($info=array(),$field_null=array()){
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		$_str='';
		$cloudsn = explode('*', $info['cloudsn']);
		switch (intval($info['roleid'])){
			case 14 :
				foreach ($cloudsn as $key => $value) {
					if($key==0){
						if(empty($value)){
							$cloudsn[0] = 'LKO'.'*';
						}
					}
				}
				break;
			case 13 :
				foreach ($cloudsn as $key => $value) {
					if($key==1){
						if(empty($value)){
							$cloudsn[1] = self::get_first_char($info['province']);
						}
					}
				}
				break;
			case 12 :
				foreach ($cloudsn as $key => $value) {
					if($key==2){
						if(empty($value)){
							$cloudsn[2] = self::get_first_char($info['city']);
						}
					}
				}
				break;
			case 10 :
				foreach ($cloudsn as $key => $value) {
					if($key==3){
						if(empty($value)){
							$cloudsn[3] = self::get_first_char($info['area']);
						}
					}
				}
				break;
			case 22 :
				foreach ($cloudsn as $key => $value) {
					if($key==4){
						if(empty($value)){
							$cloudsn[4] = 'D'.$info['userid'];
						}
					}
				}
				break;
			case 15 :
				if($info['type']==1){
					foreach ($cloudsn as $key => $value) {
						if($key==5){
							if(empty($value)){
								$cloudsn[5] = 'YGS'.$info['userid'];
							}
						}
					}
				}else{
					return false;
				}
				break;
			case 20 :
				if($info['type']==2){
					foreach ($cloudsn as $key => $value) {
						if($key==5){
							if(empty($value)){
								$cloudsn[5] = 'YGD'.$info['userid'];
							}
						}
					}
				}else{
					return false;
				}
				break;
		}
		foreach ($cloudsn as $key => $value) {
			if($key==6){
				if(empty($value)){
					$cloudsn[6] = 'TJ'.$info['id'];
				}
			}else{
				if(empty($value)){
					$cloudsn[$key] = '*';
				}else{
					$cloudsn[$key] = $cloudsn[$key].'*';
				}
			}
			$_str.=$cloudsn[$key];
		}
		return $_str;
	}
	/**
	 * 生成套件包编码
	 * @param array     $info=array(
	 * 					'id' => 套件ID,
	 * 					'cloudsn' => 套件编码,
	 * 					'roleid' => 归属用户组ID,
	 * 					'type' => 套件类型,
	 * 					'province' => 省份名称,
	 * 					'city' => 城市名称,
	 * 					'area' => 区域名称,
	 * 					'userid' => 归属用户
	 * 					)
	 * @param array 	$field_null		设置不可为空的字段
	 * @return string	生成后的套件包编码
	 * */
	function build_package_sn($info=array(),$field_null=array()){
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		$_str='';
		$cloudsn = explode('*', $info['cloudsn']);
		switch (intval($info['roleid'])){
			case 14 :
				foreach ($cloudsn as $key => $value) {
					if($key==0){
						if(empty($value)){
							$cloudsn[0] = 'LKO'.'*';
						}
					}
				}
				break;
			case 13 :
				foreach ($cloudsn as $key => $value) {
					if($key==1){
						if(empty($value)){
							$cloudsn[1] = self::get_first_char($info['province']);
						}
					}
				}
				break;
			case 12 :
				foreach ($cloudsn as $key => $value) {
					if($key==2){
						if(empty($value)){
							$cloudsn[2] = self::get_first_char($info['city']);
						}
					}
				}
				break;
			case 10 :
				foreach ($cloudsn as $key => $value) {
					if($key==3){
						if(empty($value)){
							$cloudsn[3] = self::get_first_char($info['area']);
						}
					}
				}
				break;
			case 22 :
				foreach ($cloudsn as $key => $value) {
					if($key==4){
						if(empty($value)){
							$cloudsn[4] = 'D'.$info['userid'];
						}
					}
				}
				break;
			case 15 :
				if($info['type']==1){
					foreach ($cloudsn as $key => $value) {
						if($key==5){
							if(empty($value)){
								$cloudsn[5] = 'YGS'.$info['userid'];
							}
						}
					}
				}else{
					return false;
				}
				break;
			case 20 :
				if($info['type']==2){
					foreach ($cloudsn as $key => $value) {
						if($key==5){
							if(empty($value)){
								$cloudsn[5] = 'YGD'.$info['userid'];
							}
						}
					}
				}else{
					return false;
				}
				break;
		}
		foreach ($cloudsn as $key => $value) {
			if($key==6){
				if(empty($value)){
					$cloudsn[6] = 'TJB'.$info['id'];
				}
			}else{
				if(empty($value)){
					$cloudsn[$key] = '*';
				}else{
					$cloudsn[$key] = $cloudsn[$key].'*';
				}
			}
			$_str.=$cloudsn[$key];
		}
		return $_str;
	}
	/**
	 * 生成虚拟包
	 * @param array     $info=array(
	 * 					'userid' => 虚拟包归属用户Id,
	 * 					'roleid' => 虚拟包归属用户组Id,
	 * 					'arr_id' => 套件ID数组,
	 * 					'number' => 套件数量,
	 * 					'type' => 套件类型,
	 * 					'province' => 省份id,
	 * 					'city' => 城市id,
	 * 					'area' => 区域id,
	 * 					'action_userid' => 操作用户,
	 * 					'remark' => 备注
	 * 					)
	 * @param array 	$field_null		设置不可为空的字段
	 * @return int		虚拟包编号
	 * */
	function build_virtual_package($info=array(),$field_null=array()){
		$this->dao = M('KitProduct');
		unset($this->post);
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		$arr_id = explode('|', $info['arr_id']);
		if(count($arr_id)!=intval($info['number'])){
			return $this->_err("套件打包数和虚拟包标准不符!");
		}
		//插入一条虚拟包信息
		$this->post['userid'] = $info['userid'];
		$this->post['type'] = $info['type'];
		$this->post['province'] = $info['province'];
		$this->post['city'] = $info['city'];
		$this->post['area'] = $info['area'];
		$this->post['isclose'] = 3;
		$this->post['remark'] = $info['arr_id'];
		$resid = $this->insert();
		if(false !== $resid){
			//插入当前包归属用户
			$vesting['kitid'] = $resid;
			$vesting['type'] = $info['roleid'];
			$vesting['userid'] = $info['userid'];
			$vesting['ctime'] = time();
			$this->insert_ext($this->daovesting,$vesting);
			//插入日志
			$log['kitid'] = $resid;
			$log['type'] = 1;
			$log['action_userid'] = $info['action_userid'];
			$log['ctime'] = time();
			if(!empty($info['remark'])){
				$log['remark'] = $remark;
			}
			$this->insert_ext($this->daolog,$log);
			//把虚拟包相关套件的套件类型修改为 4
			foreach ($arr_id as $key => $value) {
				$kit_type['type'] = 4;
				$this->update_ext($this->dao,$kit_type, $value);
			}
			$res=array('id'=>$resid);
			return $this->_suc($res,'生成虚拟包成功');
		}else{
			return $this->_err("生成虚拟包失败请重试");
		}
	}
	/**
	 * 解除虚拟包
	 * @param array     $info=array(
	 * 					'id' => 虚拟包ID,
	 * 					'arr_id' => 套件ID数组,
	 * 					'action_userid' => 操作用户
	 * 					)
	 * @param array 	$field_null		设置不可为空的字段
	 * @return int		更新状态
	 * */
	function unbuild_virtual_package($info=array(),$field_null=array()){
		$this->dao = M('KitProduct');
		unset($this->post);
		$this->infoid = $info['id'];
		foreach ($field_null as $key => $value) {
			if(empty($info[$key])){
				return $this->_err($value."不能为空");
			}
		}
		$arr_id = explode('|', $info['arr_id']);
		//更新虚拟包状态 5已解包
		$this->post['id'] = $this->infoid;
		$this->post['isclose'] = 5;
		$this->post = $info;
		$status = $this->update();
		if(false !== $status){
			//更新相关套件类型为 1云购商
			foreach ($arr_id as $key => $value) {
				$kit_type['type'] = 1;
				$this->update_ext($this->dao,$kit_type, $value);
			}
			//记录日志
			$log['kitid'] = $this->infoid;
			$log['type'] = 5;
			$log['action_userid'] = $action_userid;
			$log['ctime'] = time();
			if($remark != null){
				$log['remark'] = $remark;
			}
			$this->insert_ext($this->daolog,$log);
			$res=array('mStatus'=>$status);
			return $this->_suc($res,'解除虚拟包成功');
		}else{
			return $this->_err("解除虚拟包失败请重试");
		}
	}
	/**
	 * 校验用户名是否合法
	 * */
	private function _check($fn,$val){
		$model = $this->dao;
        $rel = $model->where($fn."='".$val."'")->find();
        if($rel !== false || !empty($rel)){
            return $rel;
        }else{
            return false;
        }
	}
	/**
     * 查询某个字段是否存在
	 * @param $fn 表字段名
	 * @param $val 值
     * @return 如果存在返回false，不存在返回true
     */
    private function field_exist($fn,$val){
        $model = $this->dao;
        $rel = $model->where($fn."='".$val."'")->find();
		//$this->debug = $model->getlastsql();
        if($rel === false || empty($rel) || $rel['id'] == $this->infoid){
            return true;
        }else{
            return false;
        }
    }
    /**
	 *请求接口类型异常
	 *@date 2012-05-06
	 *@time 上午08:10:19
	 */
	private function _err($str="暂无相关信息") {
		if($this->isecho == 1){
			//请求接口类型异常
			$res = array("status"=>0,"msg"=>$str,'data'=>$this->debug,'err_code'=>$this->err_code);
			if($this->isdebug==1){
				echo '<br>输出的数据:<br><textarea rows="15" cols="120">';
			}
			echo $this->myjson($res);
			if($this->isdebug==1){
				echo '</textarea>';
			}
			die();
		}else{
			$res = array("status"=>0,"msg"=>$str,'data'=>$this->debug,'err_code'=>$this->err_code);
			return $this->myjson($res);
		}
		
	}//end _err
	
	/**
	 *请求成功返回
	 *@date 2012-05-06
	 *@time 上午08:10:19
	 */
	private function _suc($arr=array(),$str="操作成功") {
		if($this->isecho == 1){
			//请求接口类型异常
			$res = array("status"=>1,"msg"=>$str,'data'=>$this->debug);
			if(!empty($arr)){
				$res=array_merge($res,$arr);
			}
			if($this->isdebug==1){
				echo '<br>输出的数据:<br><textarea rows="15" cols="120">';
			}
			echo $this->myjson($res);
			if($this->isdebug==1){
				echo '</textarea>';
			}
			die();
		}else{
			$res = array("status"=>1,"msg"=>$str,'data'=>$this->debug);
			if(!empty($arr)){
				$res=array_merge($res,$arr);
			}
			return $this->myjson($res);
		}
		
	}//end _suc
	/**
	 * 对url或其它字符转义
	 * */
	private function myjson($code) {
		$code = json_encode ( $this->urlencodeAry ( $code ) );
		return urldecode ( $code );
		//return $code;
	}
	private function urlencodeAry($data) {
		if (is_array ( $data )) {
			foreach ( $data as $key => $val ) {
				$data [$key] = $this->urlencodeAry ( $val );
			}
			return $data;
		} else {
			return urlencode ( $data );
		}
	}
	
	/**
	 * 操作插入数据表
	 * */
	private function insert(){
		$model = $this->dao;
		$_POST = $this->post;
		if (false === $model->create ()) {
			return false;
		}
		$result = $model->add ();
		if ($result !== false) {
			return $result;
		} else {
			return false;
		}
	}
	/**
	 * 操作更新数据表
	 * */
	private function update(){
		$model = $this->dao;
		if (false === $model->create ($this->post)) {
			return false;
		}
		$result = $model->save ();
		//$this->debug = $model->getlastsql();
		if (false !== $result) {
			return $result;
		} else {
			return false;
		}
	}
	/**
	 * 操作删除数据表
	 * */
	private function delete(){
		$model = $this->dao;
		$result = $model->delete ($this->infoid);
		if (false !== $result) {
			return $result;
		} else {
			return false;
		}
	}
	/**
	 * 操作查询数据表
	 * @param $id 查询用户ID
	 * @return 如果存在返回用户信息，不存在返回false，参数$id空返回所有用户信息
	 * */
	private function select($id){
		$model = $this->dao;
		if(!empty($id)){
			$result = $model->find ($id);
			return $result;
		}else{
			$result = $model->select ();
			return $result;
		}
	}
	/**
	 * 操作添加用户关联扩展或附属数据表
	 * @param $table_ext 自定义表名
	 * @param $field_value 自定义插入字段和数值
     * @return 如果成功返回相关信息，失败在返回false
	 * */
	private function insert_ext($table_ext,$field_value = array()){
		if(!empty($table_ext)){
			$this->dao = $table_ext;
		}
		$this->post = $field_value;
		$rel = $this->insert();
		return $rel;
	}
	/**
	 * 操作更新用户关联扩展或附属数据表
	 * @param $table_ext 自定义表名
	 * @param $field_value 自定义插入字段和数值
     * @return 如果成功返回相关信息，失败在返回false
	 * */
	private function update_ext($table_ext,$field_value = array(),$id){
		if(!empty($table_ext)){
			$this->dao = $table_ext;
		}
		if(!empty($id)){
			$field_value['id'] = $id;
			$this->post = $field_value;
		}
		$rel = $this->update();
		return $rel;
	}
	/**
	 * 操作删除用户关联扩展或附属数据表
     * @return 如果成功返回相关信息，失败在返回false
	 * */
	private function delete_ext($id){
		if(!empty($id)){
			$this->infoid = $id;
		}
		$rel = $this->delete();
	}
	/**
	 * 操作查询用户关联扩展或附属数据表
	 * @param $table_ext 自定义表名
	 * @param $field_value 自定义查询条件
	 * @param $field 自定义数据字段
	 * @param $isall 是否全部查询  是：true 否：false
     * @return 如果存在返回查询信息，不存在返回false
	 * */
	private function select_ext($table_ext,$field_value = array(),$field = "*",$isall = true){
		if(!empty($table_ext)){
			$this->dao = $table_ext;
		}
		$rel = $this->select_all($field_value,$field,$isall);
		return $rel;
	}
	/**
     * 自定义查询条件查询数据
	 * @param array $field_value 自定义查询条件
	 * @param $field 自定义数据字段
	 * @param $isall 是否全部查询  是：true 否：false
     * @return 如果存在返回所有信息，不存在返回false
     */   
    private function select_all($field_value = array(),$field = "*",$isall = true){
        $model = $this->dao;
		$where = "";
		foreach ($field_value as $key => $value) {
			if(empty($where)){
				$where = $key."='".$value."'";
			}else{
				$where .= " AND ".$key."='".$value."'";
			}
		}
		$rel = array();
		if($isall == true){
			$rel = $model->field($field)->where($where)->select();
			//$this->debug = $model->getlastsql();
		}else{
			$rel = $model->field($field)->where($where)->find();
			//$this->debug = $model->getlastsql();
		}
        if(!empty($rel)){//$rel !== false ||
            return $rel;
        }else{
            return false;
        }
    }
	/**
     * 查询用户是否存在
	 * @param $fn 表字段名
	 * @param $val 值
     * @return 如果存在返回一条用户信息，不存在返回false
     */
    private function select_field($fn,$val){
        $model = $this->dao;
        $rel = $model->where($fn."='".$val."'")->find();
        if($rel !== false || !empty($rel)){
            return $rel;
        }else{
            return false;
        }
    }
	
	/**
	 * 中文字符串获取每个字拼音的第一个字母大写,只能是utf-8格式的
	 * @param	string	$_String	中文字符串
	 * @return	string	中文字符串拼音大写缩写
	 * */
	private function get_first_char($_String){
		$arr = self::_StringToArray($_String);
		$_str = '';
		foreach ($arr as $key => $value) {
			$_str.=self::daxie(self::Pinyin($value,'UTF8'));
		}
		return $_str;
	}
	/**
	 * 汉字转拼音
	 * @param	string	$_String	中文字符串或单个中文字
	 * @param	string	$_code		gb2312 UTF8
	 * @return	string 返回字符拼音
	 * */
	private function Pinyin($_String, $_Code = 'gb2312') {
		$_DataKey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha" . "|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|" . "cuan|cui|cun|cuo|da|dai|dan|dang|dao|de|deng|di|dian|diao|die|ding|diu|dong|dou|du|duan|dui|dun|duo|e|en|er" . "|fa|fan|fang|fei|fen|feng|fo|fou|fu|ga|gai|gan|gang|gao|ge|gei|gen|geng|gong|gou|gu|gua|guai|guan|guang|gui" . "|gun|guo|ha|hai|han|hang|hao|he|hei|hen|heng|hong|hou|hu|hua|huai|huan|huang|hui|hun|huo|ji|jia|jian|jiang" . "|jiao|jie|jin|jing|jiong|jiu|ju|juan|jue|jun|ka|kai|kan|kang|kao|ke|ken|keng|kong|kou|ku|kua|kuai|kuan|kuang" . "|kui|kun|kuo|la|lai|lan|lang|lao|le|lei|leng|li|lia|lian|liang|liao|lie|lin|ling|liu|long|lou|lu|lv|luan|lue" . "|lun|luo|ma|mai|man|mang|mao|me|mei|men|meng|mi|mian|miao|mie|min|ming|miu|mo|mou|mu|na|nai|nan|nang|nao|ne" . "|nei|nen|neng|ni|nian|niang|niao|nie|nin|ning|niu|nong|nu|nv|nuan|nue|nuo|o|ou|pa|pai|pan|pang|pao|pei|pen" . "|peng|pi|pian|piao|pie|pin|ping|po|pu|qi|qia|qian|qiang|qiao|qie|qin|qing|qiong|qiu|qu|quan|que|qun|ran|rang" . "|rao|re|ren|reng|ri|rong|rou|ru|ruan|rui|run|ruo|sa|sai|san|sang|sao|se|sen|seng|sha|shai|shan|shang|shao|" . "she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|" . "tan|tang|tao|te|teng|ti|tian|tiao|tie|ting|tong|tou|tu|tuan|tui|tun|tuo|wa|wai|wan|wang|wei|wen|weng|wo|wu" . "|xi|xia|xian|xiang|xiao|xie|xin|xing|xiong|xiu|xu|xuan|xue|xun|ya|yan|yang|yao|ye|yi|yin|ying|yo|yong|you" . "|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|" . "zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo";
		$_DataValue = "-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990" . "|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725" . "|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263" . "|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003" . "|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697" . "|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211" . "|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922" . "|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468" . "|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664" . "|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407" . "|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959" . "|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652" . "|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369" . "|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128" . "|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914" . "|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645" . "|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149" . "|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087" . "|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658" . "|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340" . "|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888" . "|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585" . "|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847" . "|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055" . "|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780" . "|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274" . "|-10270|-10262|-10260|-10256|-10254";
		$_TDataKey = explode ( '|', $_DataKey );
		$_TDataValue = explode ( '|', $_DataValue );
		$_Data = (PHP_VERSION >= '5.0') ? array_combine ( $_TDataKey, $_TDataValue ) : self::_Array_Combine ( $_TDataKey, $_TDataValue );
		arsort ( $_Data );
		reset ( $_Data );
		if ($_Code != 'gb2312')
			$_String = self::_U2_Utf8_Gb ( $_String );
		$_Res = '';
		for($i = 0; $i < strlen ( $_String ); $i ++) {
			$_P = ord ( substr ( $_String, $i, 1 ) );
			if ($_P > 160) {
				$_Q = ord ( substr ( $_String, ++ $i, 1 ) );
				$_P = $_P * 256 + $_Q - 65536;
			}
			$_Res .= self::_Pinyin ( $_P, $_Data );
		}
		return preg_replace ( "/[^a-z0-9]*/", '', $_Res );
	}
	private function _Pinyin($_Num, $_Data) {
		if ($_Num > 0 && $_Num < 160)
			return chr ( $_Num );
		elseif ($_Num < - 20319 || $_Num > - 10247)
		return '';
		else {
			foreach ( $_Data as $k => $v ) {
				if ($v <= $_Num)
					break;
			}
			return $k;
		}
	}
	private function _U2_Utf8_Gb($_C) {
		$_String = '';
		if ($_C < 0x80)
			$_String .= $_C;
		elseif ($_C < 0x800) {
			$_String .= chr ( 0xC0 | $_C >> 6 );
			$_String .= chr ( 0x80 | $_C & 0x3F );
		} elseif ($_C < 0x10000) {
			$_String .= chr ( 0xE0 | $_C >> 12 );
			$_String .= chr ( 0x80 | $_C >> 6 & 0x3F );
			$_String .= chr ( 0x80 | $_C & 0x3F );
		} elseif ($_C < 0x200000) {
			$_String .= chr ( 0xF0 | $_C >> 18 );
			$_String .= chr ( 0x80 | $_C >> 12 & 0x3F );
			$_String .= chr ( 0x80 | $_C >> 6 & 0x3F );
			$_String .= chr ( 0x80 | $_C & 0x3F );
		}
		return iconv ( 'UTF-8', 'GB2312//IGNORE', $_String );
	}
	private function _Array_Combine($_Arr1, $_Arr2) {
		for($i = 0; $i < count ( $_Arr1 ); $i ++)
			$_Res [$_Arr1 [$i]] = $_Arr2 [$i];
		return $_Res;
	}
	/**
	 * 字符串第一个字母转大写
	 * @param $str
	 * @return 返回第一个字母大写
	 * */
	private function daxie($str){
		return substr(ucfirst($str),0,1);
	}
	/**
	 * 把字符串转成数组，支持汉字，只能是utf-8格式的
	 * @param $str
	 * @return array
	 */
	private function _StringToArray($str)
	{
	    $result = array();
	    $len = strlen($str);
	    $i = 0;
	    while($i < $len){
	        $chr = ord($str[$i]);
	        if($chr == 9 || $chr == 10 || (32 <= $chr && $chr <= 126)) {
	            $result[] = substr($str,$i,1);
	            $i +=1;
	        }elseif(192 <= $chr && $chr <= 223){
	            $result[] = substr($str,$i,2);
	            $i +=2;
	        }elseif(224 <= $chr && $chr <= 239){
	            $result[] = substr($str,$i,3);
	            $i +=3;
	        }elseif(240 <= $chr && $chr <= 247){
	            $result[] = substr($str,$i,4);
	            $i +=4;
	        }elseif(248 <= $chr && $chr <= 251){
	            $result[] = substr($str,$i,5);
	            $i +=5;
	        }elseif(252 <= $chr && $chr <= 253){
	            $result[] = substr($str,$i,6);
	            $i +=6;
	        }
	    }
	    return $result;
	}
}